package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingcartMapper;
import com.sky.service.ShoppingCartService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingcartMapper shoppingcartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
       shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart= shoppingcartMapper.getexit(shoppingCart);
        if(shoppingCart !=null){
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingcartMapper.getbynumberupdate(shoppingCart);
            return;
        }
        Long dishId=shoppingCartDTO.getDishId();

        ShoppingCart shoppingcart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingcart);
        if(dishId !=null){
            Dish dish=dishMapper.selectbyid(shoppingCartDTO.getDishId());
            shoppingcart.setAmount(dish.getPrice());
            shoppingcart.setName(dish.getName());
            shoppingcart.setImage(dish.getImage());
        }else {
            Setmeal setmeal=setmealMapper.getById(shoppingCartDTO.getSetmealId());
            System.out.println(setmeal);
            shoppingcart.setName(setmeal.getName());
            shoppingcart.setImage(setmeal.getImage());
            shoppingcart.setAmount(setmeal.getPrice());
        }
        shoppingcart.setNumber(1);
        shoppingcart.setCreateTime(LocalDateTime.now());
        shoppingcart.setUserId(BaseContext.getCurrentId());
        shoppingcartMapper.insert(shoppingcart);
    }

    @Override
    public List<ShoppingCart> shoppingcartlist() {
        return shoppingcartMapper.getshow( BaseContext.getCurrentId());
    }

    @Override
    public void subcart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart exitdata= shoppingcartMapper.getexit(shoppingCart);
        if(exitdata.getNumber()==1 && exitdata!=null ){
            shoppingcartMapper.delete(exitdata);
        }else {
            exitdata.setNumber(exitdata.getNumber()-1);
            shoppingcartMapper.getbynumberupdate(exitdata);
        }
    }

    @Override
    public void clean() {
        shoppingcartMapper.clean(BaseContext.getCurrentId());
    }
}
